//
//  XSMYViewController.m
//  MeiLiYiChu
//
//  Created by mac on 16/2/23.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "XSMYViewController.h"
#import "XSHTTPManager.h"
#import "Masonry.h"
#import "XSLoginViewController.h"
#import "XSSetViewController.h"
#import "XSLogin.h"
@interface XSMYViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView * _table;
    NSArray * _array;
    NSArray * _orderArr;
    NSArray * _addressArr;
    NSArray * _productArr;
    NSArray * _scoreArr;
    NSArray * _settingArr;
    NSArray * _bigArr;
    
    NSArray * _ordTitleArr;
    NSArray * _payArr;
    NSArray * _addTitleArr;
    NSArray * _proTitleArr;
    NSArray * _scoTitleArr;
    NSArray * _setTitleArr;
    NSArray * _titles;
    CGRect rect;
    NSInteger currentag;
}
@end

@implementation XSMYViewController
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden =YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self arrays];
   
    _table = [[UITableView alloc]initWithFrame:CGRectMake(0, _topView.frame.size.height, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStyleGrouped];
    _table.delegate =self;
    _table.dataSource =self;

    [self.view addSubview:_table];

}
#pragma mark -数组群
-(void)arrays
{
    _array =[NSArray arrayWithObjects:@"user_sendment",@"user_payment",@"user_recement",@"user_recevied",@"user_refundment", nil];
    _orderArr =[NSArray arrayWithObjects:@"user_order",_array, nil];
    _addressArr =[NSArray arrayWithObjects:@"user_address",@"user_coupons",@"user_image_wallet",@"user_image_ykcoin",@"user_image_sign", nil];
    _productArr =[NSArray arrayWithObjects:@"user_product",@"user_activity",@"user_brand",@"user_album",@"user_share",@"user_focus",@"user_fans", nil];
    _scoreArr =[NSArray arrayWithObjects:@"user_score2", nil];
    _settingArr =[NSArray arrayWithObjects:@"user_setting", nil];
    _bigArr =[NSArray arrayWithObjects:_orderArr,_addressArr,_productArr,_scoreArr,_settingArr, nil];
    
    
    _payArr =[NSArray arrayWithObjects:@"待付款",@"待发货",@"待收货",@"已收货",@"退款中", nil];
    _ordTitleArr =[NSArray arrayWithObjects:@"我的订单",_payArr, nil];
    
    _addTitleArr =[NSArray arrayWithObjects:@"我的收货地址",@"我的优惠券",@"我的钱包",@"我的美丽币",@"签到中心", nil];
    _proTitleArr =[NSArray arrayWithObjects:@"我的宝贝",@"我的主题",@"我的品牌",@"我的专辑",@"我的分析",@"我的关注",@"我的粉丝", nil];
    _scoreArr =[NSArray arrayWithObjects:@"给美丽衣橱打5分", nil];
    _setTitleArr =[NSArray arrayWithObjects:@"设置", nil];
    _titles =@[_ordTitleArr,_addTitleArr,_proTitleArr,_scoreArr,_setTitleArr];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _titles.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (section ==0)
    {
        return _orderArr.count;
    }
    else
    {
        NSArray * array =_bigArr[section];
        return array.count;
    }
    
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString * cellIndenti =@"Cell";

    UITableViewCell * cell =[_table dequeueReusableCellWithIdentifier:cellIndenti];
    //在之前清空重用
    for (int i =0; i< _array.count; i++)
    {
        UIImageView * imageView =(UIImageView *)[cell.contentView viewWithTag:i+20];
        [imageView removeFromSuperview];
        UILabel * lab =(UILabel *)[cell.contentView viewWithTag:i+30];
        [lab removeFromSuperview];
        UIButton * button =(UIButton *)[cell.contentView viewWithTag:i+10];
        [button removeFromSuperview];
    }
    
    if (!cell)
    {
        cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIndenti];
   
    }
    //配置区域
    cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;

    
    cell.textLabel.text =@"";
    cell.imageView.image =[UIImage imageNamed:@""];

    
    if (indexPath.section ==0)
    {
        if (indexPath.row ==0)
        {
            cell.textLabel.text =_ordTitleArr[0];
            cell.imageView.image =[UIImage imageNamed:_orderArr[0]];
        }
        else
        {
            cell.accessoryType =UITableViewCellAccessoryNone;
            
            UIButton * lastBtn;
           for (int i =0; i< _array.count; i++)
           {
               
               UIButton*  button2 = [UIButton buttonWithType:UIButtonTypeCustom];
               button2.tag =i+10;
               currentag =button2.tag;
               [button2 addTarget:self action:@selector(payClick:) forControlEvents:UIControlEventTouchUpInside];
               
               [cell.contentView addSubview:button2];
              
               
               UIImageView*  imageView2 =[[UIImageView alloc]init];
               imageView2.tag =i+20;
               
               imageView2.image =[UIImage imageNamed:_array[i]];
                   
               
               [cell.contentView addSubview:imageView2];
               UILabel *  lab2 =[[UILabel alloc]init];
               lab2.tag =i+30;
               lab2.text =_payArr[i];
               [cell.contentView addSubview:lab2];

               [button2 mas_makeConstraints:^(MASConstraintMaker *make) {
                   make.top.offset(0);
                   make.bottom.offset(0);
                   if (!lastBtn) {
                       make.left.offset(10);
                   }
                   else
                   {
                       make.left.equalTo(lastBtn.mas_right).with.offset(0);
                       make.width.equalTo(lastBtn.mas_width);
                   }
                   if (i==4) {
                       make.right.offset(0);
                   }
               }];
               
               [imageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
                   make.top.offset(20);
                   make.bottom.offset(-30);
                   make.centerX.equalTo(button2.mas_centerX);
                   make.width.equalTo(@40);
               }];
               
               [lab2 mas_makeConstraints:^(MASConstraintMaker *make) {
                   make.top.offset(70);
                   make.bottom.offset(10);
                   make.centerX.equalTo(button2.mas_centerX);
                   make.width.equalTo(@60);
               }];
               lastBtn =button2;
            
           }
        }
    }
    else
    {
        
        NSArray * array =_bigArr[indexPath.section];
        
        cell.imageView.image =[UIImage imageNamed:array[indexPath.row]];
        
        NSArray * titleArr =_titles[indexPath.section];
        cell.textLabel.text = titleArr[indexPath.row];
        
    }
    if (indexPath.section ==1)
    {
        if (indexPath.row ==2)
        {
//            if ([XSHTTPManager isLogin]) {
////                cell.detailTextLabel.text =[NSString stringWithFormat:@"可用余额 %@%@",];
             cell.detailTextLabel.text =@"可用余额 ￥0";
        }
        
        else if (indexPath.row==3)
        {
//            if ([XSHTTPManager isLogin]) {
//                //                cell.detailTextLabel.text =[NSString stringWithFormat:@"可用余额 %@%@",];
//            }

            cell.detailTextLabel.text =@"10枚";

        }
    }

    return cell;
}

////区头高度
-(CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section ==0)
    {
        
        return 150;
        
    }
    return 10;
}

//自定义区头
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section ==0)
    {
        _topView =[[UIView alloc]initWithFrame:CGRectMake(0, -250, self.view.bounds.size.width, 400)];
        
        UIImageView * imageView =[[UIImageView alloc]init];
        imageView.frame =CGRectMake(0, -250, self.view.bounds.size.width, 400);
        
        imageView.image =[UIImage imageNamed:@"userspace_top_bg.jpg"];
        [_topView addSubview:imageView];
        //如果登录过
//        if (![XSHTTPManager isLogin]) {
        
            UILabel * lab =[[UILabel alloc]init];
            lab.center = CGPointMake(_topView.center.x, _topView.center.y+100);
            lab.bounds =CGRectMake(0, 0, 240, 20);
            lab.text =@"登录美丽衣橱,有神秘惊喜哦!";
            [_topView addSubview:lab];
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.center =CGPointMake(_topView.center.x, lab.center.y+40);
            button.bounds=CGRectMake(0, 0, 120, 40);
            button.backgroundColor = [UIColor magentaColor];
            [button setTitle:@"登录" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
            [_topView addSubview:button];
//        }
//        else
//        {
//            
//            XSLogin * login =_userArr[0];
//            
//            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//            button.center =CGPointMake(_topView.center.x, _topView.center.y+100);
//            button.bounds=CGRectMake(0, 0, 80, 80);
//            [button setBackgroundImage:[UIImage imageNamed:login.user_image_url] forState:UIControlStateNormal];
//            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//            [button addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
//            [_topView addSubview:button];
//            
//            UILabel * lab =[[UILabel alloc]init];
//            lab.center = CGPointMake(_topView.center.x, button.center.y+40);
//            lab.bounds =CGRectMake(0, 0, 240, 20);
//            lab.text =login.user_name;
//            [_topView addSubview:lab];
//
//        }
        

        return _topView;
        
    }
    else
    {
        return 0;
    }
    
}
-(void)login
{
    XSLoginViewController *loginVC =[[XSLoginViewController alloc]init];
    loginVC.block =^(NSMutableArray *array){
        _userArr =array;
    };
    
    self.hidesBottomBarWhenPushed =YES;
    [self.navigationController pushViewController:loginVC animated:YES];
    self.hidesBottomBarWhenPushed =NO;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section ==0)
    {
        if (indexPath.row ==1)
        {
            return 100;
        }
    }
   
        return 44;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //判断是不是已经登录
//    if () {
//        
//    }
    if (indexPath.section ==4)
    {
        if (indexPath.row==0)
        {
            XSSetViewController * setVC =[[XSSetViewController alloc]init];
            self.hidesBottomBarWhenPushed =YES;
            [self.navigationController pushViewController:setVC animated:YES];
            self.hidesBottomBarWhenPushed =NO;
            
        }
    }
    else
    {
        if (indexPath.section ==0)
        {
            if (indexPath.row==1)
            {
                
            }
        }
        XSLoginViewController * login =[[XSLoginViewController alloc]init];
        self.hidesBottomBarWhenPushed =YES;
        [self.navigationController pushViewController:login animated:YES];
        self.hidesBottomBarWhenPushed =NO;
    }

    

}
-(void)payClick:(UIButton *)btn
{
    NSLog(@"btnClick");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
